package com.scaffold.authority.action;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.authority.DbResourceFactory;
import com.scaffold.authority.entity.Resource;
import com.scaffold.authority.service.ResourceService;

@Controller
public class ResourceJsonAction extends BasicBizAction{
	@Autowired
	private ResourceService resourceService;
	
	@RequestMapping("/resource/import.json")
	public String imp(String text) throws IOException{
		if(text!=null && text.length()>0){
			resourceService.imp(text);	
		}
		return this.getSuccessResult();
	}
	
	@RequestMapping("/resource/list.json")
	public String list(Integer type, String value, String keyword){
		Page page = this.getPage(10);
		List<Resource> list = resourceService.pageQuery(page, type, value, keyword);
		return this.getPageResult(page, list);
	}
	
	@RequestMapping("/resource/save.json")
	public String save(Long id){
		if(id==null){
			Resource resource = new Resource();
			this.bindParams(resource);
			this.setOwner(resource);
			this.resourceService.save(resource);
		}else{
			Resource resource = this.resourceService.getResource(id);
			this.checkOwner(resource);
			this.bindParams(resource);
			this.resourceService.update(resource);
		}
		return this.getSuccessResult();
	}

	@RequestMapping("/resource/get.json")
	public String get(Long id){
		Resource resource = resourceService.getResource(id);
		this.checkOwner(resource);
		return this.getSuccessResult(resource);
	}

	@RequestMapping("/resource/delete.json")
	public String delete(Long id){
		Resource resource = resourceService.getResource(id);
		this.checkOwner(resource);
		this.resourceService.delete(resource);
		return this.getSuccessResult();
	}

	@RequestMapping("/resource/batchDelete.json")
	public String batchDelete(String idseq){
		this.resourceService.batchDelete(idseq);
		return this.getSuccessResult();
	}

	@RequestMapping("/resource/select.json")
	public String select(Integer type, String value, String keyword){
		List<Resource> list = resourceService.pageQuery(null, type, value, keyword);
		return this.getSuccessResult(this.getOptions(list, "value"));
	}

	@RequestMapping("/resource/setting/saveAuthoritys.json")
	public String saveAuthoritys(Long resourceId, String authorityId) {
		Resource resource = this.resourceService.getResource(resourceId);
		this.checkOwner(resource);
		resourceService.saveAuthoritys(resourceId, authorityId);
		return this.getSuccessResult();
	}

	@RequestMapping("/resource/setting/getAuthorityIdList.json")
	public String getAuthorityIdList(Long resourceId) {
		Resource resource = this.resourceService.getResource(resourceId);
		this.checkOwner(resource);
		List<Long> list = resourceService.getAuthorityIdList(resourceId);
		return this.getSuccessResult(list);
	}


	@Autowired
	private DbResourceFactory resourceFactory;
	
	@RequestMapping("/resource/reload.json")
	public String reload(){
		resourceFactory.reload();
		return this.getSuccessResult();
	}
	
}
